package homework_JDBC.zhangmu.view;

import homework_JDBC.zhangmu.dao.AdminDao;
import homework_JDBC.zhangmu.dao.AdminDaoImpl;
import homework_JDBC.zhangmu.dao.ZhangmuDao;
import homework_JDBC.zhangmu.dao.ZhangmuDaoImpl;
import static homework_JDBC.zhangmu.uitl.Read.getIn;

import java.util.List;
import java.util.Map;

public class UserView {
    AdminDao adminDao=new AdminDaoImpl();
    ZhangmuDao zhangmuDao=new ZhangmuDaoImpl();
    public Map<String, Object> login() {
        Map<String, Object> map=null;
        System.out.print("用户名称:");
        String name=getIn().next();
        System.out.print("用户密码:");
        String pwd=getIn().next();
        map=adminDao.login(name,pwd);
        return map;
    }
    public void  menu(){
        System.out.println("1.添加账目信息");
        System.out.println("2.修改账目信息");
        System.out.println("3.查询账目信息");
        System.out.println("4.删除账目信息");
        int n=getIn().nextInt();
        switch (n){
            case 1:
                addzhangmu();
                break;
            case 2:
                updatezhangmu();
                break;
            case 3:
                searchzhangmu();
                break;
            case 4:
                deletezhangmu();
                break;
            default:
                System.out.println("请按要求输入选项");

        }
    }

    private void deletezhangmu() {
        String id=getIn().next();
        int r=zhangmuDao.deletezhangmu(id);
        if (r>0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    private void searchzhangmu() {
        System.out.println("1.查询所有账目信息");
        System.out.println("2.查询某个时间段的账目信息");
        int n=getIn().nextInt();
        switch (n){
            case 1:
                searchzhangmuAll();
                break;
            case 2:
                searchzhangmuByDate();
                break;
        }
    }

    private void searchzhangmuByDate() {
        String date1=getIn().next();
        String date2=getIn().next();
        List<Map<String,Object>> list=zhangmuDao.searchzhangmuByDate(date1,date2);
        System.out.println("账目编号\t账目类型\t账目内容\t金额\t日期");
        for (int i=0;i<list.size();i++){
            Map<String,Object> map=list.get(i);
            Object id=map.get("zhangmu_id");
            Object type=map.get("zhangmu_type");
            Object name=map.get("zhangmu_name");
            Object money=map.get("zhangmu_money");
            Object date=map.get("zhangmu_date");
            System.out.println(id+"\t"+type+"\t"+name+"\t"+money+"\t"+date);
        }
    }

    private void searchzhangmuAll() {
        List<Map<String,Object>> list=zhangmuDao.searchzhangmuAll();
        System.out.println("账目编号\t账目类型\t账目内容\t金额\t日期");
        for (int i=0;i<list.size();i++){
            Map<String,Object> map=list.get(i);
            Object id=map.get("zhangmu_id");
            Object type=map.get("zhangmu_type");
            Object name=map.get("zhangmu_name");
            Object money=map.get("zhangmu_money");
            Object date=map.get("zhangmu_date");
            System.out.println(id+"\t"+type+"\t"+name+"\t"+money+"\t"+date);
        }
    }

    private void updatezhangmu() {
        System.out.println("要修改的账目编号：");
        String id=getIn().next();
        System.out.println("账目类型：");
        String type=getIn().next();
        System.out.println("账目内容：");
        String name=getIn().next();
        System.out.println("金额：");
        String money=getIn().next();
        System.out.println("日期：");
        String date=getIn().next();
        int r=zhangmuDao.updatezhangmu(type,name,money,date,id);
        if (r>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }

    private void addzhangmu() {
        System.out.println("账目类型：");
        String type=getIn().next();
        System.out.println("账目内容");
        String name=getIn().next();
        System.out.println("金额");
        String money=getIn().next();
        System.out.println("日期");
        String date=getIn().next();
        int r=zhangmuDao.addzhangmu(type,name,money,date);
        if (r>0){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
}
